package server

import (
	"6-5zk/common/models/mysql"
	"6-5zk/common/models/redis"
	"6-5zk/common/proto/user"
	"context"
	"encoding/json"
	"errors"
	"gorm.io/gorm"
	"time"
)

func TakeFoodList(req *user.TakeFoodListReq) (*user.TakeFoodListResp, error) {
	list, err := mysql.TakeFoodList()
	if err != nil {
		return nil, errors.New("查询失败")
	}
	var lists []*user.List
	for _, food := range list {
		lists = append(lists, &user.List{
			OrderNum: int64(food.OrderNum),
			UserNum:  int64(food.UserNum),
			ShopNum:  int64(food.ShopNum),
			RiderNum: int64(food.RiderNum),
		})
		marshal, err := json.Marshal(&lists)
		if err != nil {
			return nil, err
		}
		redis.Rdb.Set(context.Background(), "takefoodlist", marshal, time.Minute*30)
	}
	return &user.TakeFoodListResp{List: lists}, nil
}

func NoticeCreate(req *user.NoticeCreateReq) (*user.NoticeCreateResp, error) {
	notice := mysql.Notice{
		Model:        gorm.Model{},
		NoticeStatus: int(req.NoticeStatus),
		Client:       int(req.Client),
		Title:        req.Title,
		Content:      req.Content,
		Connect:      req.Connect,
		Image:        req.Image,
	}
	err := mysql.NoticeCreate(&notice)
	if err != nil {
		return nil, errors.New("消息发送失败")
	}
	return &user.NoticeCreateResp{Success: "消息发送成功"}, nil
}
